package com.johnson.news;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.baidu.android.pushservice.PushMessageReceiver;
import com.google.gson.Gson;
import com.johnson.news.activity.AskmeReplyDetailActivity;
import com.johnson.news.activity.NewsDetailActivity;
import com.johnson.news.model.bean.NewsItem;
import com.johnson.news.model.bean.PushMessage;
import com.johnson.news.model.util.NewsPreference;
import com.johnson.news.util.Utils;

import java.util.List;

public class NewsPushReceiver extends PushMessageReceiver {

    NewsPreference pref ;

    private enum MESSAGE_CATEGORY{
        HOT_NEWS_MSG,ASKME_REPLY_MSG,GROUP_MSG
    }

    @Override
    public void onBind(Context arg0, int errorCode, String appId, String userId,
                       String channelId, String requestId) {
        // TODO Auto-generated method stub
        pref = NewsPreference.getinstance(arg0);
        pref.setPushChannelId(channelId);
        Utils.channelId = channelId;
    }

    @Override
    public void onDelTags(Context arg0, int arg1, List<String> arg2,
                          List<String> arg3, String arg4) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onListTags(Context arg0, int arg1, List<String> arg2,
                           String arg3) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onMessage(Context arg0, String arg1, String arg2) {
        // TODO Auto-generated method stub
        Log.e("NewsReceiver", "onMessage");
    }

    @Override
    public void onNotificationArrived(Context arg0, String title, String description,
                                      String content) {
        // TODO Auto-generated method stub
        Log.e("NewsReceiver", "onNotificationArrived");
    }

    @Override
    public void onNotificationClicked(Context context, String title, String description,
                                      String contentString) {
        // TODO Auto-generated method stub
        Log.e("NewsReceiver", "onNotificationClicked");
        handleIncomingMessage(context, contentString);
    }

    @Override
    public void onSetTags(Context arg0, int arg1, List<String> arg2,
                          List<String> arg3, String arg4) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onUnbind(Context arg0, int arg1, String arg2) {
        // TODO Auto-generated method stub
    }

    private void handleIncomingMessage(Context context,String contentJson){
//        if(TextUtils.isEmpty(contentJson)) {
//            contentJson = "{\n" +
//                    "\t\"itemid\": \"6009\",\n" +
//                    "\t\"catid\": 426,\n" +
//                    "\t\"type\": \"1\",\n" +
//                    "\t\"alt\": null,\n" +
//                    "\t\"mid\": 31\n" +
//                    "}";
//        }
        if(Utils.isEmptyString(contentJson)){
            return;
        }
        Gson gson = new Gson();
        PushMessage pushMessage = gson.fromJson(contentJson, PushMessage.class);
        if(pushMessage == null){
            return;
        }
        if(pushMessage.getType().equals("1") || pushMessage.getType().equals("3")){
            NewsItem newsItem = new NewsItem();
            newsItem.setItemid(pushMessage.getItemid());
            newsItem.setCatid(pushMessage.getCatid());
            Intent intent = new Intent(context, NewsDetailActivity.class);
//            intent.putExtra(NewsDetailActivity.intent_key_from_push, true);
//            intent.putExtra(NewsDetailActivity.intent_key_is_hot, pushMessage.getType().equals("1"));
//            intent.putExtra(NewsDetailActivity.intent_key_mid, pushMessage.getMid());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Bundle bundle = new Bundle();
            bundle.putBoolean(NewsDetailActivity.intent_key_from_push, true);
            bundle.putBoolean(NewsDetailActivity.intent_key_is_hot, pushMessage.getType().equals("1"));
            bundle.putString(NewsDetailActivity.intent_key_mid, pushMessage.getMid());
            bundle.putParcelable("data", newsItem);
            intent.putExtras(bundle);
            context.startActivity(intent);
//            Utils.openNewsContent(context, pushMessage.getItemid(),
//                    pushMessage.getCatid(), "",true,true,"");
        }else if(pushMessage.getType().equals("2")){
//            Utils.openAskmeReply(context, pushMessage.getItemid(),
//                    pushMessage.getBt(),true,false);
            Intent intent = new Intent(context, AskmeReplyDetailActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("itemid", pushMessage.getItemid());
            bundle.putString("title", pushMessage.getBt());
            bundle.putBoolean("fromPush", true);
            intent.putExtras(bundle);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }


}
